package com.example.neutonote2.data.dao;

import androidx.lifecycle.LiveData;
import androidx.room.*;
import androidx.room.RewriteQueriesToDropUnusedColumns;

import com.example.neutonote2.data.model.Tag;
import com.example.neutonote2.data.model.NoteTagCrossRef;

import java.util.List;

/**
 * 标签数据访问对象
 */
@Dao
public interface TagDao {
    @Insert
    long insert(Tag tag);

    @Update
    void update(Tag tag);

    @Delete
    void delete(Tag tag);

    @Query("SELECT * FROM tags ORDER BY name ASC")
    LiveData<List<Tag>> getAllTags();

    @Query("SELECT * FROM tags ORDER BY name ASC")
    List<Tag> getAllTagsSync();

    @Query("SELECT * FROM tags WHERE id = :id")
    LiveData<Tag> getTagById(int id);
    
    @Insert
    void insertNoteTagCrossRef(NoteTagCrossRef crossRef);
    
    @Delete
    void deleteNoteTagCrossRef(NoteTagCrossRef crossRef);
    
    @Query("DELETE FROM note_tag_cross_ref WHERE noteId = :noteId")
    void deleteAllTagsForNote(int noteId);
    
    @RewriteQueriesToDropUnusedColumns
    @Query("SELECT * FROM tags " +
           "INNER JOIN note_tag_cross_ref ON tags.id = note_tag_cross_ref.tagId " +
           "WHERE note_tag_cross_ref.noteId = :noteId")
    LiveData<List<Tag>> getTagsForNote(int noteId);
} 